// pages/print/baiduCloud/baiduCloud.js
var utils = require("../../../utils/utils.js");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        picFixShow: false,
        list: [],
        firstOpen: 0,
        lastPath: '',
        lastName: '',
        key: '',
        pathArray:[''],
        serviceSubtypeId:0
    },
    backNav() {
        wx.navigateBack()
    },
      seejx(e){
        var url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: '/pages/my/manualDetail/manualDetail?id='+url+'&name=如何上传好友分享的文档',
          })
    },
    clpi() {
        wx.setClipboardData({
            data: '百度网盘授权',
            success(res) {
                wx.getClipboardData({
                    success(res) {}
                })
            }
        })
    },
    //获取下级目录
    getNextFir(e) {
        var that = this;
        var dir = e.currentTarget.dataset.dir;
        var name = e.currentTarget.dataset.name;
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('kdocs/get', {
            dir: dir
        }, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                if (res.data.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                } else {
                    var num = 0;
                    var sARRY =[]
                    for(var i=0;i<that.data.pathArray.length;i++){
                        if(that.data.pathArray[i]==dir){
                            num++
                        }
                    }
                    if(num==0){
                        sARRY.push(dir)
                    }
                    // var lastPath = dir.replace(name, "")
                    // if (lastPath.substring(0, 1) == '/' && lastPath.length > 1) {
                    //     lastPath = lastPath.substring(0, lastPath.length - 1);
                    // } else {
                    //     lastPath = '/' + lastPath.substring(0, lastPath.length - 1);
                    // }
                    that.setData({
                        list: res.data.data.lists,
                        lastName: name,
                        pathArray:that.data.pathArray.concat(sARRY)
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    addKey(e) {
        this.setData({
            key: e.detail.value
        })
    },
    //打开授权界面
    openaAuth(appid,path) {
        wx.navigateToMiniProgram({
            appId:appid, //金山文档小程序id
            path:path,
            success(res) {

            }
        })
    },
    //获取用户文件列表
    getIndex() {
        var that = this;
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('kdocs/get', {code:app.globalData.wpscode||''}, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                if (res.data.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                    that.openaAuth(res.data.data.authUrl.appId,res.data.data.authUrl.path)
                } else {
                    app.globalData.wpscode = '';
                    that.setData({
                        list: res.data.data.lists,
                        infoUrl:res.data.data.helpId
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    //设置个空白方法，阻止冒泡
    touchAnother() {

    },
    //点击文件获取链接下载
    printGo(e) {
        var that = this;
        var id = that.data.chooseIndex;
        if(!utils.is_define(id)){
            utils.toast('请先选择要打印的文件哦')
            return false;
        }
        var name = that.data.chooseName;
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('kdocs/get', {
            fileToken: id,
            sn: wx.getStorageSync('printId'),
            fileName:name,
            code:app.globalData.wpscode||''
        }, function (res) {
            if (res.data.code == 200) {
                if (res.data.data.isNeedAuth == 1) {
                    that.selectComponent("#loading").hideLoadingCus();
                    that.setData({
                        lastPath: ''
                    })
                } else {
                    app.globalData.wpscode = '';
                    var reg = /^.*\.(?:xls|xlsx)$/i;
                    if(reg.test(res.data.data.fileName)){
                      this.excelTpdf(res.data.data.fileName,res.data.data.fileUrl)
                    }else{
                      //非excel
                    //    wx.showLoading()
                       that.kUploadFile(res.data.data.fileName,res.data.data.fileUrl)
                    }
                    // if (Array.isArray(res.data.data)) {
                    //     wx.navigateTo({
                    //         url: '/pages/print/chooseSheet/chooseSheet?list=' + JSON.stringify(res.data.data) + '&code=28120290',
                    //     })
                    // } else {
                    //     var xsk = res.data.data.fileUrl.split('.');
                    //     if (xsk[xsk.length - 1] == 'xlsx' || xsk[xsk.length - 1] == 'xls') {
                    //         var array = [];
                    //         res.data.data.downUrl = res.data.data.fileUrl;
                    //         res.data.data.sheetName = res.data.data.filename.replace(/.xlsx/, "").replace(/.xls/, "");
                    //         array.push(res.data.data)
                    //         wx.navigateTo({
                    //             url: '/pages/print/chooseSheet/chooseSheet?list=' + JSON.stringify(array) + '&code=28120290',
                    //         })
                    //     } else {
                    //         wx.navigateTo({
                    //             url: '../list/list?code=28120290&name=' + res.data.data.filename + '&icon=' + res.data.data.fileIcon + '&path=' + res.data.data.fileUrl + '&pageNum=' + res.data.data.pageNum,
                    //         })
                    //     }
                    // }
                }
            } else {
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg)
            }
        })
    },
          //excel转pdf
  excelTpdf(path,name) {
    var that = this;
    utils.getSheetByUrl(name,path,function (res) {
       var data = res.data.data
    //    wx.hideLoading()
     that.selectComponent("#loading").hideLoadingCus();
       wx.navigateTo({
        url: '/pages/print/chooseSheet/chooseSheet?list='+escape(JSON.stringify(data))+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId='+that.data.serviceSubtypeId+'&tagFloor='+that.data.tagFloor+'&code='+that.data.code,
      })
    })
  },
    kUploadFile(name,fileUrl){
        var that = this;
        utils.uploadfile('',name,fileUrl,'','',function (res) {
            var data = res.data.data
             that.selectComponent("#loading").hideLoadingCus();
            wx.navigateTo({
            url: '../list/list?code=' + that.data.code + '&name=' + data.localName + '&path=' + data.printUrl + '&pageNum=' + data.pageTotal +'&fileUrl='+data.fileUrl+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId='+that.data.serviceSubtypeId+'&tagFloor='+that.data.tagFloor+'&orientation='+data.orientation
            })
         })
    },
    chooseIndex(e){
        var id = e.currentTarget.dataset.id;
        var name = e.currentTarget.dataset.name;
        var chooseIndex = this.data.chooseIndex
        this.setData({
            chooseIndex:chooseIndex==id?'':id,
            chooseName:chooseIndex==id?'':name,
        })
    },
    // 返回上一层
    goFirst() {
        var that = this;
        var paths = that.data.pathArray;
        paths.pop()
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('kdocs/get', {
            dir: paths[paths.length-1],
            code:app.globalData.wpscode||''
        }, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                if (res.data.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                } else {
                    // app.globalData.wpscode = '';
                    // if (dir == '') {
                    //     dir = ''
                    // } else {
                    //     var newStr = dir.split("/")
                    //     dir = dir.replace('/' + newStr[newStr.length - 1], "")
                    //     if (dir == '') {
                    //         dir = '/'
                    //     }
                    // }
                    that.setData({
                        list: res.data.data.lists,
                        pathArray: paths
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    hidePicFix() {
        this.setData({})
    },
    // 金山授权
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.getIndex();
        wx.getSystemInfo({
            success: (result) => {
              var model = result.model;
              if(model=='iPhone X'){
                  that.setData({
                    pdbottom:'68',
                    serviceMainId: options.serviceMainId,
                    code: options.code,
                    serviceSubtypeId: options.serviceSubtypeId,
                    tagFloor: options.tagFloor
                  })
              }else{
                that.setData({
                    pdbottom:'40',
                    serviceMainId: options.serviceMainId,
                    code: options.code,
                    serviceSubtypeId: options.serviceSubtypeId,
                    tagFloor: options.tagFloor
                })
              }
            },
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        if (that.data.firstOpen == 1) {
            this.getIndex();
        } else {
            that.setData({
                firstOpen: 1
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})